package edu.wong.pro.serviceImpl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Session;
import org.hibernate.Transaction;

import edu.wong.pro.dao.PictureDAO;
import edu.wong.pro.dao.PictureShowDAO;
import edu.wong.pro.iservice.IPicture;
import edu.wong.pro.model.Picture;
import edu.wong.pro.model.PictureShow;
import edu.wong.pro.other.SaveFile;

public class PictureService implements IPicture{

	@Override
	public void savePictures(File[] files, String[] imageFileName,
    String[] imageContentType, String PSTheme, String PSAddtion) {
		// TODO Auto-generated method stub
			int psid = savePictureShow(PSTheme, PSAddtion);
		try{
			ServletActionContext.getRequest().setCharacterEncoding("UTF-8");
		 	String savePath = ServletActionContext.getServletContext().getRealPath("/picture/");

	        if (files !=null && files.length > 0) {
	            for (int i = 0; i < files.length; i++) {
	            	//保存图片路径在数据库
	            	
	            	//建立上传文件的输出流, getImageFileName()[i]
	            	savePicture(psid, "picture/"+psid+"_"+imageFileName[i]);
	            //采用多线程保存的图片
	            	SaveFile saveFile = new SaveFile(files[i], savePath + "\\" +psid+"_"+imageFileName[i]);
	            	Thread thread = new Thread(saveFile);
	            	thread.start();
	            	
	             //   System.out.println(savePath + "\\" + imageFileName[i]);
//	                FileOutputStream fos = new FileOutputStream(savePath + "\\" +psid+"_"+imageFileName[i]);
//	                //建立上传文件的输入流
//	                FileInputStream fis = new FileInputStream(files[i]);
//	                byte[] buffer = new byte[1024];
//	                int len = 0;
//	                while ((len=fis.read(buffer))>0) {
//	                    fos.write(buffer, 0, len);
//	                }
//	                fos.close();
//	                fis.close();
	            }
	        }
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	//保存图片集
	private int savePictureShow(String PSTheme,String PSAddtion){
		HttpSession httpSession = ServletActionContext.getRequest().getSession();
		int memId = (Integer)httpSession.getAttribute("memberID");
		PictureShowDAO dao = new PictureShowDAO();
		Session  session = dao.getSession();
		Transaction transaction = session.beginTransaction();
		PictureShow pShow = new PictureShow();
		pShow.setMemId(memId);
		pShow.setPsaddition(PSAddtion);
		pShow.setPstheme(PSTheme);
		dao.save(pShow);
		transaction.commit();
		session.close();
		return pShow.getPsid();
	}
	//保存图片
	private void savePicture(int PSID,String picPath){
		PictureDAO dao = new PictureDAO();
		Session session = dao.getSession();
		Transaction transaction = session.beginTransaction();
		Picture picture = new Picture();
		picture.setPsid(PSID);
		picture.setPicPath(picPath);
		dao.save(picture);
		transaction.commit();
		session.close();	
	}

	@Override
	public JSONArray getPictureShow() {
		// TODO Auto-generated method stub
		PictureShowDAO dao = new PictureShowDAO();
		return JSONArray.fromObject(dao.findAll());
	}

	@Override
	public JSONArray getPictureByPsid(String psid) {
		// TODO Auto-generated method stub
	    PictureDAO dao = new PictureDAO();
		return JSONArray.fromObject(dao.findByProperty("psid", Integer.parseInt(psid)));
	}

	@Override
	public JSONObject getPictureShowByID(String psid) {
		// TODO Auto-generated method stub
		PictureShowDAO dao = new PictureShowDAO();
		return JSONObject.fromObject(dao.findById(Integer.parseInt(psid)));
	}
}
